{% comment %}
This code includes the content of a page after massaging it to fix the
header levels to shift them down one level, make the links relative,
and change "section" to "sub-section" and "page" to "section" in the content.
Note: This last step is fragile. It could have adverse effects since
this is merely text based.
{% endcomment %}

{% unless include.noshift %}
   {% assign fixedcontent = include.content | replace: "# ", "## " %}
   {% capture fixedcontent %}{{fixedcontent | replace: "<h4", "<h5"}}{% endcapture %}
   {% capture fixedcontent %}{{fixedcontent | replace: "</h4>", "</h5>"}}{% endcapture %}
   {% capture fixedcontent %}{{fixedcontent | replace: "<h3", "<h4"}}{% endcapture %}
   {% capture fixedcontent %}{{fixedcontent | replace: "</h3>", "</h4>"}}{% endcapture %}
   {% capture fixedcontent %}{{fixedcontent | replace: "<h2", "<h3"}}{% endcapture %}
   {% capture fixedcontent %}{{fixedcontent | replace: "</h2>", "</h3>"}}{% endcapture %}
   {% capture fixedcontent %}{{fixedcontent | replace: "<h1", "<h2"}}{% endcapture %}
   {% capture fixedcontent %}{{fixedcontent | replace: "</h1>", "</h2>"}}{% endcapture %}
{% else %}
   {% assign fixedcontent = include.content %}
{% endunless %}

{% for file in include.files %}
   {%comment%}[Link](baseurl/file) -> [Link](#file){%endcomment%}
   {% capture pattern %}]({{include.baseurl}}{{file}}{% endcapture %}
   {% capture anchor %}](#{{file}}{% endcapture %}
   {% capture fixedcontent %}{{fixedcontent | replace: pattern, anchor}}{% endcapture %}
   {%comment%}[Link](file) -> [Link](#file){%endcomment%}
   {% capture pattern %}]({{file}}{% endcapture %}
   {% capture anchor %}](#{{file}}{% endcapture %}
   {% capture fixedcontent %}{{fixedcontent | replace: pattern, anchor}}{% endcapture %}
   {%comment%}[Link](#file#) -> [Link](#){%endcomment%}
   {% capture pattern %}](#{{file}}#{% endcapture %}
   {% capture fixedcontent %}{{fixedcontent | replace: pattern, "](#"}}{% endcapture %}

   {%comment%}[Link]: baseurl/file -> [Link]: #file{%endcomment%}
   {% capture pattern %}]: {{include.baseurl}}{{file}}{% endcapture %}
   {% capture anchor %}]: #{{file}}{% endcapture %}
   {% capture fixedcontent %}{{fixedcontent | replace: pattern, anchor}}{% endcapture %}
   {%comment%}[Link]: file -> [Link]: #file{%endcomment%}
   {% capture pattern %}]: {{file}}{% endcapture %}
   {% capture anchor %}]: #{{file}}{% endcapture %}
   {% capture fixedcontent %}{{fixedcontent | replace: pattern, anchor}}{% endcapture %}
   {%comment%}[Link]: #file# -> [Link]: #{%endcomment%}
   {% capture pattern %}]: #{{file}}#{% endcapture %}
   {% capture fixedcontent %}{{fixedcontent | replace: pattern, "]: #"}}{% endcapture %}

   {%comment%}href="baseurl/file -> href="#file{%endcomment%}
   {% capture pattern %}href="{{include.baseurl}}{{file}}{% endcapture %}
   {% capture anchor %}href="#{{file}}{% endcapture %}
   {% capture fixedcontent %}{{fixedcontent | replace: pattern, anchor}}{% endcapture %}
   {%comment%}href="file -> href="#file{%endcomment%}
   {% capture pattern %}href="{{file}}{% endcapture %}
   {% capture anchor %}href="#{{file}}{% endcapture %}
   {% capture fixedcontent %}{{fixedcontent | replace: pattern, anchor}}{% endcapture %}
   {%comment%}href="#file# -> href="#{%endcomment%}
   {% capture pattern %}href="#{{file}}#{% endcapture %}
   {% capture anchor %}href="#{% endcapture %}
   {% capture fixedcontent %}{{fixedcontent | replace: pattern, anchor}}{% endcapture %}

   {%comment%}Links to index page / baseurl{%endcomment%}
   {%comment%}[Link](baseurl){%endcomment%}
   {% capture pattern %}]({{include.baseurl}}){% endcapture %}
   {% capture anchor %}]({{page.url}}){% endcapture %}
   {% capture fixedcontent %}{{fixedcontent | replace: pattern, anchor}}{% endcapture %}
   {%comment%}We do not know how to handle this form: [Link]: baseurl
   Changing this will screw up all other links starting with baseurl
   {% capture pattern %}]: {{include.baseurl}}{% endcapture %}
   {% capture anchor %}]: {{page.url}}{% endcapture %}
   {% capture fixedcontent %}{{fixedcontent | replace: pattern, anchor}}{% endcapture %}
   {%endcomment%}
   {%comment%}href="index -> href="thispage{%endcomment%}
   {% capture pattern %}href="index{% endcapture %}
   {% capture anchor %}href="{{page.url}}{% endcapture %}
   {% capture fixedcontent %}{{fixedcontent | replace: pattern, anchor}}{% endcapture %}
   {%comment%}href="baseurl" -> href="thispage"{%endcomment%}
   {% capture pattern %}href="{{include.baseurl}}"{% endcapture %}
   {% capture anchor %}href="{{page.url}}"{% endcapture %}
   {% capture fixedcontent %}{{fixedcontent | replace: pattern, anchor}}{% endcapture %}
{% endfor %}
{% capture fixedcontent %}{{fixedcontent | replace: " section", " subsection" | replace: "Section", "Subsection" }}{% endcapture %}
{{fixedcontent | replace: " page", " section" | replace: "Page", "Section" }}
